package org.algorithm.biubiu.wheel_1.字符串;

import org.junit.Test;

import java.util.Stack;

/**
 * @author jiazhaoxin
 * @Date 2022/2/27
 * @Desc
 */
public class 有效的括号 {


    @Test
    public  void  test2(){
        boolean valid = isValid("()");
    }

    public boolean isValid(String data) {
        if (data == null) {
            return false;
        }
        Stack<Character> stack = new Stack();
        for (int i = 0; i < data.length(); i++) {
            char r = data.charAt(i);
            if ('(' == (r) || '[' == (r) || '{' == (r)) {
                stack.add(r);
            }
            if (!stack.isEmpty()) {
                if (')' == r &&'(' != stack.pop()) {
                    return false;
                }
                if (']' == (r)&& '[' != stack.pop()) {
                    return false;
                }

                if ('}' == (r) && '{' != stack.pop()) {
                    return false;
                }
            }else {
                return false;
            }

        }
        return stack.isEmpty();
    }
}
